//
//  ByViewGoodsInfo.m
//  mijifen_ios
//
//  Created by mc on 2017/11/1.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewGoodsInfo.h"

@interface ByViewGoodsInfo()<UITextFieldDelegate>
@property (nonatomic, strong) NSMutableDictionary *infoDic;
@end

@implementation ByViewGoodsInfo
{
    UIView      *_bgView;
    UILabel     *_priceLabel;
    UILabel     *_priceFlagLabel;
    UITextField *_priceTf;
    
    UIView      *_lineColView;
    
    UILabel     *_originPriceLabel;
    UILabel     *_originFlagLabel;
    UITextField *_originTf;
    
    UIView      *_lineRowView;
    
    UILabel     *_stockLabel;
    UITextField *_stockTf;
    
    UIView      *_lineColView2;
    
    UILabel     *_warnLabel;
    UITextField *_warnTf;
    
}

#pragma mark - getter
- (NSMutableDictionary *)infoDic{
    if (!_infoDic) {
        _infoDic = [NSMutableDictionary dictionary];
    }
    return _infoDic;
}


- (instancetype)init{
    if (self = [super init]) {
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _bgView = [[UIView alloc] init];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_top);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 100));
    }];
    _bgView.backgroundColor = [UIColor whiteColor];
    
    // 价格
    CGFloat width = [@"价格：" getSizeOfString:16].width;
    CGFloat height = 30;
    _priceLabel = [[UILabel alloc] init];
    [_bgView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgView.mas_left).offset(10);
        make.top.equalTo(_bgView.mas_top).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    _priceLabel.text = @"价格：";
    _priceLabel.font = FONT(16);
    
    width = [@"¥" getSizeOfString:16].width;
    _priceFlagLabel = [[UILabel alloc] init];
    [_bgView addSubview:_priceFlagLabel];
    [_priceFlagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgView.mas_right).offset(-(SCREEN_WIDTH / 2 - 10));
        make.top.equalTo(_priceLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    _priceFlagLabel.text = @"¥";
    _priceFlagLabel.font = FONT(16);
    
    _priceTf = [[UITextField alloc] init];
    [_bgView addSubview:_priceTf];
    [_priceTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_priceLabel.mas_right).offset(5);
        make.top.equalTo(_priceLabel.mas_top);
        make.right.equalTo(_priceFlagLabel.mas_left);
        make.height.mas_equalTo(@(height));
    }];
    _priceTf.font = FONT(16);
    _priceTf.textAlignment = NSTextAlignmentRight;
    _priceTf.delegate = self;
    _priceTf.tag = 10;
    
    _lineColView = [[UIView alloc] init];
    [_bgView addSubview:_lineColView];
    [_lineColView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_priceLabel.mas_top);
        make.left.equalTo(_priceFlagLabel.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(1, height));
    }];
    _lineColView.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    

    
    // 原价
    width = [@"原价：" getSizeOfString:16].width;
    _originPriceLabel = [[UILabel alloc] init];
    [_bgView addSubview:_originPriceLabel];
    [_originPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lineColView.mas_right).offset(5);
        make.top.equalTo(_lineColView.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    _originPriceLabel.text = @"原价：";
    _originPriceLabel.font = FONT(16);
    
    width = [@"¥" getSizeOfString:16].width;
    _originFlagLabel = [[UILabel alloc] init];
    [_bgView addSubview:_originFlagLabel];
    [_originFlagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_bgView.mas_right).offset(-10);
        make.top.equalTo(_originPriceLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    _originFlagLabel.text = @"¥";
    _originFlagLabel.font = FONT(16);
    
    _originTf = [[UITextField alloc] init];
    [_bgView addSubview:_originTf];
    [_originTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_originFlagLabel.mas_left);
        make.top.equalTo(_originFlagLabel.mas_top);
        make.left.equalTo(_originPriceLabel.mas_right).offset(5);
        make.height.mas_equalTo(@(height));
    }];
    _originTf.textAlignment = NSTextAlignmentRight;
    _originTf.font = FONT(16);
    _originTf.delegate = self;
    _originTf.tag = 11;
    
    // 线
    _lineRowView = [[UIView alloc] init];
    [_bgView addSubview:_lineRowView];
    [_lineRowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_priceLabel.mas_bottom).offset(10);
        make.left.equalTo(_bgView.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 20, 1));
    }];
    _lineRowView.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    
    // 库存
    width = [@"库存：" getSizeOfString:16].width;
    _stockLabel = [[UILabel alloc] init];
    [_bgView addSubview:_stockLabel];
    [_stockLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgView.mas_left).offset(10);
        make.top.equalTo(_lineRowView.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    _stockLabel.text = @"库存：";
    _stockLabel.font = FONT(16);
    
    _stockTf = [[UITextField alloc] init];
    [_bgView addSubview:_stockTf];
    [_stockTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_stockLabel.mas_right).offset(5);
        make.top.equalTo(_stockLabel.mas_top);
        make.right.equalTo(_lineColView.mas_left).offset(-5);
        make.height.mas_equalTo(@(height));
    }];
    _stockTf.textAlignment = NSTextAlignmentRight;
    _stockTf.placeholder = @"选填";
    _stockTf.font = FONT(16);
    _stockTf.delegate = self;
    _stockTf.tag = 12;
    
    _lineColView2 = [[UIView alloc] init];
    [_bgView addSubview:_lineColView2];
    [_lineColView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_stockLabel.mas_top);
        make.left.equalTo(_lineColView.mas_left);
        make.size.mas_equalTo(CGSizeMake(1, height));
    }];
    _lineColView2.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    
    width = [@"库存预警：" getSizeOfString:16].width + 5;
    _warnLabel = [[UILabel alloc] init];
    [_bgView addSubview:_warnLabel];
    [_warnLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lineColView2.mas_right).offset(5);
        make.top.equalTo(_lineColView2.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    _warnLabel.text = @"库存预警：";
    _warnLabel.font = FONT(16);
    
    _warnTf = [[UITextField alloc] init];
    [_bgView addSubview:_warnTf];
    [_warnTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_bgView.mas_right).offset(-10);
        make.top.equalTo(_warnLabel.mas_top);
        make.left.equalTo(_warnLabel.mas_right).offset(5);
        make.height.mas_equalTo(@(height));
    }];
    _warnTf.placeholder = @"选填";
    _warnTf.textAlignment = NSTextAlignmentRight;
    _warnTf.font = FONT(16);
    _warnTf.delegate = self;
    _warnTf.tag = 13;
    _warnTf.adjustsFontSizeToFitWidth = YES;

}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag == 10) {
        [self.infoDic setValue:textField.text forKey:@"price"];
    }else if (textField.tag == 11){
        [self.infoDic setValue:textField.text forKey:@"originPrice"];
    }else if (textField.tag == 12){
        [self.infoDic setValue:textField.text forKey:@"stock"];
    }else{
        [self.infoDic setValue:textField.text forKey:@"stockWarn"];
    }
    
    if (self.editBlock) {
        self.editBlock(self.infoDic);
    }
}
@end
